<?php
/**
 * @filename ProgressController.php 
 * @encoding UTF-8 
 * @author KangShuAn
 * @datetime 2014-12-27 17:10:48
 * @version 1.0
 * @Description项目进度
 */
class ProgressController extends AdminController
{
    /**
     * 项目进度查询
     * 即此每个项目下问卷进度的平均值
     */
    public function actionSchedule(){
        $p = $this->get('p', 1);
        $project_id = $this->get('project_id');
        $criteria = new CDbCriteria();
        if ($this->project_id != -1) {
            $criteria->addInCondition('project_id', explode(',', $this->project_id));
        }
        if ($project_id) {
            $criteria->addColumnCondition(array('project_id' => $project_id));
        }
        $criteria->addCondition('status=1');

        # 控制搜索条件
        $params['project_name'] = $this->get('project_name', '');
        if($params['project_name']){
                $criteria->addSearchCondition('project_name', $params['project_name']);
        }

        $count = Project::model()->count($criteria);

        $criteria->limit = $this->limit;
        $criteria->offset = ($p - 1) * $this->limit;
        $criteria->order = 'project_id DESC';
        $data = Project::model()->findAll($criteria);
        //项目下的已完成的问卷数量
        if ($data) {
            $question = new QuestionBase();
            foreach ($data as $key => $val) {
                $data[$key]['complete_num'] = $question->count('project_id='.intval($val['project_id']).' and status=100');
                //查看问卷数量，和库里question_num对标，不符的更新
                $data[$key]['question_num'] = $question->count('project_id='.intval($val['project_id']).' and status!=0');
                if ($data[$key]['question_num'] != $val['question_num']) {
                    Project::model()->updateByPk($val['project_id'], array('question_num' => $data[$key]['question_num']));
                }
            } 
        }
        $this->smarty->assign('list', $data);
        $page = new Page($count, $this->limit);
        $this->smarty->assign('params', $params);
        $this->smarty->assign('page', $page->style1());
        $this->smarty->display('progress/schedule.tpl');
    }

    /**
     * 项目中问卷进度列表
     */
    public function actionProjectQuestion(){
            $p = $this->request('p', 1);
            $params = array();
            $params['project_id'] = $this->request('project_id');
            $params['question_name'] = $this->request('question_name', '');
            $params['question_identifier'] = $this->request('question_identifier', '');
            if (empty($params['project_id'])) {
                $this->jump('无项目ID');
            }
            # 获取项目信息
            $objProject = new Project();
            $projectInfo = $objProject->findByPk($params['project_id']);
            
            # 根据搜索条件获取项目下问卷
            $objQuestion = new QuestionBase();
            $criteria = new CDbCriteria();
            $criteria->addInCondition('status', array(1,2,100));
            $criteria->addCondition('project_id='.$params['project_id']);
            if ($this->project_id != -1) {
                $criteria->addInCondition('project_id', explode(',', $this->project_id));
            }
            
            if($params['question_name']){
                    $criteria->addSearchCondition('question_name', $params['question_name']);
            }
            if($params['question_identifier']){
                    $criteria->addSearchCondition('identifier', $params['question_identifier']);
            }

            $count = $objQuestion->count($criteria);

            $criteria->limit = $this->limit;
            $criteria->offset = ($p - 1) * $this->limit;
            $criteria->order = 'question_id DESC';
            $questionInfo = $objQuestion->findAll($criteria);
            if ($questionInfo) {
                foreach ($questionInfo as $key => $val) {
                    //已导入数据
                    $questionInfo[$key]['complete_num'] = NetworkQuestion::model()->count("question_id={$val['question_id']} and complete_num!=0 and status in(1,2)");
                    //网点数量
                    $questionInfo[$key]['network_num'] = NetworkQuestion::model()->count("question_id={$val['question_id']} and status in(1,2)");
                    //提交到总行的网点数量
                    $questionInfo[$key]['tijiao_one_num'] = NetworkQuestion::model()->count("question_id={$val['question_id']} and status in(1,2) and node_id=1");
                }
            }

            $page = new Page($count, $this->limit);

            $this->smarty->assign('page', $page->style1());
            $this->smarty->assign('params', $params);
            $this->smarty->assign('projectInfo', $projectInfo);
            $this->smarty->assign('questionInfo', $questionInfo);
            $this->smarty->display('progress/project_quetion.tpl');

    }
    /**
     * 问卷下面，每一个网点所属问卷的进度
     * 即这个问卷下面的问题和已回答问题的比
     */
    public function actionNetQuestion()
    {
        $p = $this->get('p', 1);
        $network_name = $this->get('network_name');
        $network_num = $this->get('network_num');
        $question_id = (int)$this->get('question_id');
        $audit_s = $this->get('audit');
        //查询问卷信息
        $question = new QuestionBase();
        $info = $question->findByPk($question_id)->attributes;
        //查询网点
        $network = new NetWork();
        $criteria_net = new CDbCriteria();
        if ($network_name) {
            $criteria_net->addSearchCondition('network_name', $network_name);
        }
        if ($network_num) {
            $criteria_net->addSearchCondition('network_num', $network_num);
        }
        
        $criteria = new CDbCriteria();
        if ($network_name || $network_num) {
            $net = $network->findAll($criteria_net);
            if ($net) {
                $network_id = array();
                foreach ($net as $key => $val) {
                    $network_id[] = $val['network_id'];
                }
            }
            $criteria->addInCondition('network_id', $network_id);
        }
        
        
        $netquestion = new NetworkQuestion();
        $criteria->addColumnCondition(array('question_id' => $question_id));
        if ($audit_s!=-1) {
            $criteria->addColumnCondition(array('audit' => $audit_s));
        }
        $count = $netquestion->count($criteria);

        $criteria->limit = $this->limit;
        $criteria->offset = ($p - 1) * $this->limit;
        $criteria->order = 'create_time desc';
        $list = $netquestion->findAll($criteria);
        if ($list) {
            //网点编号
            foreach ($list as $key => $val) {
                $netinfo = $network->findByPk($val['network_id'])->attributes;
                $list[$key]['network_num'] = $netinfo['network_num'];
                $list[$key]['network_name'] = $netinfo['network_name'];
            }
        }
        $page = new Page($count, $this->limit);
        $this->smarty->assign('list', $list);
        $this->smarty->assign('page', $page->style1());
        $this->smarty->assign('question_id', $question_id);
        $this->smarty->assign('network_name', $network_name);
        $this->smarty->assign('network_num', $network_num);
        $this->smarty->assign('audit_show', NetworkQuestion::$audit_show);
        $this->smarty->assign('audit', NetworkQuestion::$audit);
        $this->smarty->assign('level_show', NetWork::networkLevel());
        $this->smarty->assign('audit_s', $audit_s);
        $this->smarty->assign('info', $info);
        $this->smarty->display('progress/network_question.tpl');
    }
	
}